package com.service.impl;

import com.bean.entity.Staff;
import com.bean.vo.PageAssistant;
import com.bean.vo.StaffQueryVo;
import com.bean.vo.StaffVo;
import com.dao.StaffMapper;
import com.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工业务实现
 */
@Service
public class StaffServiceImpl implements StaffService{

    @Autowired
    private StaffMapper staffMapper;

    @Transactional(rollbackFor = Exception.class) //配置事务管理
    @Override
    public StaffVo query(Staff staff) throws Exception {
        //查询员工
        List<StaffVo> staffVoList = staffMapper.select(staff);
        //判断员工是否存在
        if(staffVoList.size()==0)
            throw new Exception("没有该员工数据");
        //判断员工是否在职
        if(staffVoList.get(0).getState()==2)
            throw new Exception("该员工已离职!");
        //返回员工数据
        return staffVoList.get(0);
    }

    @Override
    public PageAssistant<StaffVo> queryStaffByPage(PageAssistant<StaffVo> pa, StaffQueryVo staffQueryVo) {
       Map<String ,Object>  map=new HashMap<>();
       //打印参数
        System.out.println("roleLevel："+staffQueryVo.getRoleLevel());
        //查询正常在职的员工
        map.put("state",1);
        //职务级别
        if(staffQueryVo.getRoleLevel()!=null ){
            map.put("roleLevel",staffQueryVo.getRoleLevel());
        }
        //登录员工所在部门
        if(staffQueryVo.getDeptId()!=null ){
            map.put("deptId",staffQueryVo.getDeptId());
        }
        //查询总记录数
        pa.setCount(staffMapper.selectCount(map));
        /*判断,如果count 为0--查无数据*/
        if(staffMapper.selectCount(map)==0){
            //分页助手的items修改为null
            pa.setItems(null);
            return pa;
        }

        /*添加分页参数*/
        //数据起始编号
        map.put("start_code", pa.getStartCode());
        //页面大小
        map.put("page_size", pa.getPageSize());
        /*添加查询条件*/
        //关键字查询
        if(staffQueryVo.getKeyword()!=null && !staffQueryVo.getKeyword().equals("")){
            map.put("keyword",staffQueryVo.getKeyword());
        }
        //职务id查询
        if(staffQueryVo.getRoleId()!=null){
            map.put("roleId",staffQueryVo.getRoleId());
        }
        //根据生日日期区间查询
        if(staffQueryVo.getBirthdayMin()!=null ){
            map.put("birthdayMin",staffQueryVo.getBirthdayMin());
        }

        if(staffQueryVo.getBirthdayMax()!=null){
            //将终止日期加上一天的毫秒数
            Date endDate =new Date(staffQueryVo.getBirthdayMax().getTime()+86400000);
            map.put("birthdayMax",endDate);
        }
        //根据生日月份查询
        if(staffQueryVo.getBirthMonth()!=null){

            map.put("birthMonth",staffQueryVo.getBirthMonth());
        }

//        map.put("roleId",staffQueryVo.getRoleId());
//        map.put("birthdayMin",staffQueryVo.getBirthdayMin());
//        map.put("birthdayMax",staffQueryVo.getBirthdayMax());
//        map.put("roleLevel",staffQueryVo.getRoleLevel());

        //查询正常在职的员工
//        map.put("state",1);
        //查询
        List<StaffVo> staffVo = staffMapper.selectByPage(map);
        //修改分页助手的页面内容
        pa.setItems(staffVo);
        return pa;
    }

    @Override
    public int add(Staff staff) {

        return staffMapper.insert(staff);
    }

    @Override
    public int modify(Staff staff) {
        return staffMapper.update(staff);
    }

    @Override
    public int remove(Staff staff) {
        return staffMapper.delete(staff);
    }
}
